<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>设置-个人信息</title>
    <link rel="icon" href="assets/img/favicon.ico">

    <link rel="stylesheet" type="text/css" href="css/webbase.css"/>
    <link rel="stylesheet" type="text/css" href="css/pages-seckillOrder.css"/>
</head>

<body>
<!--页面顶部白条条，由js动态加载-->
<script type="text/javascript" src="plugins/jquery/jquery.min.js"></script>
<div class="nav-bottom"></div>
<script type="text/javascript">$(".nav-bottom").load("top.html");</script>

<script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#service").hover(function () {
            $(".service").show();
        }, function () {
            $(".service").hide();
        });
        $("#shopcar").hover(function () {
            $("#shopcarlist").show();
        }, function () {
            $("#shopcarlist").hide();
        });

    })
</script>
<script type="text/javascript" src="js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="js/plugins/jquery-placeholder/jquery.placeholder.min.js"></script>
<script type="text/javascript" src="js/widget/nav.js"></script>
<script type="text/javascript" src="pages/userInfo/distpicker.data.js"></script>
<script type="text/javascript" src="pages/userInfo/distpicker.js"></script>
<script type="text/javascript" src="pages/userInfo/main.js"></script>
<!--    <script src="./js/pages/shortcut.js"></script>-->
</body>
<!--header-->
<div id="account">
    <div class="py-container">
        <div class="yui3-g home">
            <!--左侧列表-->
            <div class="yui3-u-1-6 list">

                <div class="person-info">
                    <div class="person-photo"><img src="img/_/photo.png" alt=""></div>
                    <div class="person-account">
                        <span class="name">Michelle</span>
                        <span class="safe">账户安全</span>
                    </div>
                    <div class="clearfix"></div>
                </div>
                <div class="list-items">
                    <dl>
                        <dt><i>·</i> 订单中心</dt>
                        <dd><a href="home-index.html">我的订单</a></dd>
                        <dd><a href="home-order-pay.html">待付款</a></dd>
                        <dd><a href="home-order-send.html">待发货</a></dd>
                        <dd><a href="home-order-receive.html">待收货</a></dd>
                        <dd><a href="home-order-evaluate.html">待评价</a></dd>
                    </dl>
                    <dl>
                        <dt><i>·</i> 我的中心</dt>
                        <dd><a href="home-person-collect.html">我的收藏</a></dd>
                        <dd><a href="home-person-footmark.html">我的足迹</a></dd>
                    </dl>
                    <dl>
                        <dt><i>·</i> 物流消息</dt>
                    </dl>
                    <dl>
                        <dt><i>·</i> 设置</dt>
                        <dd><a href="home-setting-info.html">个人信息</a></dd>
                        <dd><a href="home-setting-address.html" class="list-active">地址管理</a></dd>
                        <dd><a href="home-setting-safe.html">安全管理</a></dd>
                    </dl>
                </div>
            </div>
            <!--右侧主内容-->
            <div class="yui3-u-5-6">
                <div class="body userAddress">
                    <div class="address-title">
                        <span class="title">地址管理</span>
                        <a data-toggle="modal" data-target=".edit" data-keyboard="false"
                           class="sui-btn  btn-info add-new" onclick="load('')">添加新地址</a>
                        <span class="clearfix"></span>
                    </div>
                    <div class="address-detail">
                        <table class="sui-table table-bordered">
                            <thead>
                            <tr>
                                <th>姓名</th>
                                <th>地址</th>
                                <th>联系电话</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="list">
                            <tr>
                                <td>刘田田</td>
                                <td>北京 北京市 海淀区 上地街道东北旺西路8号中关村软件园9号楼</td>
                                <td>1774***********4</td>
                                <td>
                                    <a href="#">编辑</a>
                                    <a href="#">删除</a>
                                    默认地址
                                </td>
                            </tr>


                            </tbody>
                        </table>
                    </div>
                    <!--新增地址弹出层-->
                    <div tabindex="-1" role="dialog" data-hasfoot="false" class="sui-modal hide fade edit"
                         style="width:580px;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" data-dismiss="modal" aria-hidden="true" class="sui-close"
                                            id="closeForm">×
                                    </button>
                                    <h4 id="myModalLabel" class="modal-title">新增地址</h4>
                                </div>
                                <div class="modal-body">
                                    <form action="" id="address" class="sui-form form-horizontal">
                                        <div class="control-group">
                                            <label class="control-label">收货人：</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="addressee" name="addressee">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">所在地区：</label>
                                            <div class="controls">
                                                <div data-toggle="distpicker">
                                                    <!--                                                    todo-->
                                                    <div class="form-group area">
                                                        <select id="province" name="province">
                                                            <option value="0" selected="selected">-- 请选择 --</option>
                                                        </select>
                                                    </div>
                                                    <select id="city" name="city">
                                                        <option value="0" selected="selected">-- 请选择 --</option>
                                                    </select>
                                                    <div class="form-group area">
                                                        <select id="county" name="district">
                                                            <option value="0" selected="selected">-- 请选择 --</option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">详细地址：</label>
                                            <div class="controls">
                                                <input type="text" class="input-large" id="street" name="street">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">联系电话：</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="phone" name="phone">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">邮箱：</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="email" name="email">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">地址别名：</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="secondName"
                                                       name="secondName">
                                            </div>
                                            <div class="othername">
                                                建议填写常用地址：<a href="#" class="sui-btn btn-default">家里</a>　<a href="#"
                                                                                                           class="sui-btn btn-default">父母家</a>　<a
                                                    href="#" class="sui-btn btn-default">公司</a>
                                            </div>
                                        </div>

                                    </form>


                                </div>

                                <div class="modal-footer">
                                    <button type="button" data-ok="modal" class="sui-btn btn-primary btn-large"
                                            id="sub">确定
                                    </button>
                                    <button type="button" data-dismiss="modal" class="sui-btn btn-default btn-large"
                                            id="cancel">取消
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>


                    <!--                        编辑地址弹出层-->
                    <div tabindex="-1" role="dialog" data-hasfoot="false" class="sui-modal hide fade edit2"
                         style="width:580px;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" data-dismiss="modal" aria-hidden="true" class="sui-close"
                                            id="closeForm2">×
                                    </button>
                                    <h4 id="myModalLabel2" class="modal-title">修改地址</h4>
                                </div>
                                <div class="modal-body">
                                    <form action="" id="address2" class="sui-form form-horizontal">
                                        <div class="control-group">
                                            <label class="control-label">收货人：</label>
                                            <input type="hidden" id="id" name="id">
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="addressee2"
                                                       name="addressee">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">所在地区：</label>
                                            <div class="controls">
                                                <div data-toggle="distpicker">
                                                    <!--                                                    todo-->
                                                    <div class="form-group area">
                                                        <select id="province2" name="province">
                                                            <option value="0" selected="selected">-- 请选择 --</option>
                                                        </select>
                                                    </div>
                                                    <select id="city2" name="city">
                                                        <option value="0" selected="selected">-- 请选择 --</option>
                                                    </select>
                                                    <div class="form-group area">
                                                        <select id="county2" name="district">
                                                            <option value="0" selected="selected">-- 请选择 --</option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">详细地址：</label>
                                            <div class="controls">
                                                <input type="text" class="input-large" id="street2" name="street">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">联系电话：</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="phone2" name="phone">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">邮箱：</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="email2" name="email">
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">地址别名：</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="secondName2"
                                                       name="secondName">
                                            </div>
                                            <div class="othername">
                                                建议填写常用地址：<a href="#" class="sui-btn btn-default">家里</a>　<a href="#"
                                                                                                           class="sui-btn btn-default">父母家</a>　<a
                                                    href="#" class="sui-btn btn-default">公司</a>
                                            </div>
                                        </div>

                                    </form>


                                </div>

                                <div class="modal-footer">
                                    <button type="button" data-ok="modal" class="sui-btn btn-primary btn-large"
                                            id="sub2">确定
                                    </button>
                                    <button type="button" data-dismiss="modal" class="sui-btn btn-default btn-large"
                                            id="cancel2">取消
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<!-- 底部栏位 -->
<!--页面底部，由js动态加载-->
<div class="clearfix footer"></div>
<script type="text/javascript">$(".footer").load("foot.html");</script>
<!--页面底部END-->
<script>
    //编辑事件
    function edit(id) {
        // load("2");
        // alert(id)
        alert(id)
        $.ajax({
            type: "get",
            xhrFields: {withCredentials: true},
            url: "http://api.leyou.com/user/address/findById?id=" + id,
            success: function (result) {
                $("#id").val(result.id);
                $("#addressee2").val(result.addressee);
                // attr("selected",true);
                // $("#province2").val(result.province);
                let p=`<option value='${result.province}' selected='selected'>${result.provinceName}</option>`;
                console.log(p);
                $('#province2').append(p);
                let c=`<option value='${result.city}' selected='selected'>${result.cityName}</option>`;
                $("#city2").append(c);

                let conty=`<option value='${result.district}' selected='selected'>${result.districtName}</option>`
                $("#county2").append(conty);

                $("#street2").val(result.street);

                $("#phone2").val(result.phone);
                $("#email2").val(result.email);
                $("#secondName2").val(result.secondName);

            },
            dataType: "json"
        });
    }

    //点击确认
    $("#sub2").click(function () {
        // todo
        let id=$("#id").val();
        let addressee = $("#addressee2").val();
        let province = $("#province2").val();
        let city = $("#city2").val();
        let district = $("#county2").val();
        let street = $("#street2").val();
        let phone = $("#phone2").val();
        let email = $("#email2").val();
        let secondName = $("#secondName2").val();

        let parm = {
            "id":id,
            "addressee": addressee,
            "province": province,
            "city": city,
            "district": district,
            "street": street,
            "phone": phone,
            "email": email,
            "secondName": secondName
        };
        console.log(parm);

        $.ajax({
            type: "post",
            xhrFields: {withCredentials: true},
            url: "http://api.leyou.com/user/address/update",
            data: parm,
            success: function (result) {
                alert("更新成功")
            },
            dataType: "json"
        });
    });

    $(function () {
        // alert()
        // 查询地址信息
        $.ajax({
            type: "get",
            xhrFields: {withCredentials: true},
            url: "http://api.leyou.com/user/address/list",
            success: function (result) {
                // console.log(result);


                //数据封装
                showAddress(result)
            },
            dataType: "json"
        });
    });

    //渲染数据
    function showAddress(address) {
        let content = "";
        for (let addr of address) {
            console.log(addr);
            content += `<tr>
                        <td>${addr.addressee}</td>
                        <td>${addr.provinceName} ${addr.cityName} ${addr.districtName} ${addr.street}</td>
                        <td>${addr.phone}</td>
                        <td>
                            <a data-toggle="modal" data-target=".edit2" data-keyboard="false"   class="sui-btn  btn-info add-new"  onclick="edit('${addr.id}')">编辑</a>
                            <a href="#" onclick="remove('${addr.id}')">删除</a>
                            <a href="http://api.leyou.com/user/address/default?id=${addr.id}">默认地址</a>
                        </td>
                    </tr>`
        }
        $("#list").html(content);

    }


    //删除
    function remove(id) {
        // alert(id)
        $.ajax({
            type: "get",
            xhrFields: {withCredentials: true},
            url: "http://api.leyou.com/user/address/delete?id=" + id,
            success: function (result) {

                window.location.href = "http://www.leyou.com/home-setting-address.html?id=" + new Date().getTime();
                alert("成功了")
            },
            dataType: "json"
        });


    }

    //关闭
    $("#closeForm").click(
        function () {
            //清空
            clearForm();
        }
    );



    //取消
    $("#cancel").click(
        function () {
            //清空
            clearForm();
        }
    );


    $("#sub").click(function () {
        // alert()

        serializeForm();
    });

    function serializeForm() {// 参数为form标签
        // todo
        let addressee = $("#addressee").val();
        let province = $("#province").val();
        let city = $("#city").val();
        let district = $("#county").val();
        let street = $("#street").val();
        let phone = $("#phone").val();
        let email = $("#email").val();
        let secondName = $("#secondName").val();

        let parm = {
            "addressee": addressee,
            "province": province,
            "city": city,
            "district": district,
            "street": street,
            "phone": phone,
            "email": email,
            "secondName": secondName
        };

        $.ajax({
            type: "post",
            xhrFields: {withCredentials: true},
            url: "http://api.leyou.com/user/address/add",
            data: parm,
            success: function (result) {

            },
            dataType: "json"
        });
        //清空
        clearForm();
    }

    function clearForm() {
        let addressee = $("#addressee").val("");
        let province = $("#province").val(0);
        let city = $("#city").val(0);
        let district = $("#county").val(0);
        let street = $("#street").val("");
        let phone = $("#phone").val("");
        let email = $("#email").val("");
        let secondName = $("#secondName").val("");
    }

    function add() {
        console.log("123")
    }

    //加载省份数据
    function load(id) {
        console.log(id);
        $.ajax({
            type: "get",
            xhrFields: {withCredentials: true},
            url: "http://api.leyou.com/user/address/province",
            success: function (result) {
                $('#province '+id+' option').remove();
                $('#province'+id).append("<option value='0' selected='selected'>-- 请选择 --</option>");
                for (var i in result) {
                    $('#province'+id).append("<option value='" + result[i].provinceid + "'>" + result[i].province + "</option>")
                }
            }
        });
    }


    //给省份下拉列表添加监听事件，监听选择项，生成市列表
    $(function () {
        $('#province').change(function () {
            $.ajax({
                type: "get",
                xhrFields: {withCredentials: true},
                url: "http://api.leyou.com/user/address/city/" + $(this).val(),
                success: function (result) {
                    $('#city option').remove();
                    $('#city').append("<option value='0' selected='selected'>-- 请选择 --</option>");
                    for (var i in result) {
                        $('#city').append("<option value='" + result[i].cityid + "'>" + result[i].city + "</option>")
                    }
                }
            });
        });
    });



    //给省份下拉列表添加监听事件，监听选择项，生成市列表
    $(function () {
        $('#province2').change(function () {
            $.ajax({
                type: "get",
                xhrFields: {withCredentials: true},
                url: "http://api.leyou.com/user/address/city/" + $(this).val(),
                success: function (result) {
                    $('#city2 option').remove();
                    $('#city2').append("<option value='0' selected='selected'>-- 请选择 --</option>");
                    for (var i in result) {
                        $('#city2').append("<option value='" + result[i].cityid + "'>" + result[i].city + "</option>")
                    }
                }
            });
        });
    });

    //给市下拉列表添加监听事件，监听选择项，生成区县列表
    $(function () {
        $('#city').change(function () {
            $.ajax({
                type: "get",
                xhrFields: {withCredentials: true},
                url: "http://api.leyou.com/user/address/area/" + $(this).val(),
                success: function (result) {
                    $('#county option').remove();
                    $('#county').append("<option value='0' selected='selected'>-- 请选择 --</option>");
                    for (var i in result) {
                        $('#county').append("<option value='" + result[i].areaid + "'>" + result[i].area + "</option>")
                    }
                }
            });
        });
    });

    //给市下拉列表添加监听事件，监听选择项，生成区县列表
    $(function () {
        $('#city2').change(function () {
            $.ajax({
                type: "get",
                xhrFields: {withCredentials: true},
                url: "http://api.leyou.com/user/address/area/" + $(this).val(),
                success: function (result) {
                    $('#county2 option').remove();
                    $('#county2').append("<option value='0' selected='selected'>-- 请选择 --</option>");
                    for (var i in result) {
                        $('#county2').append("<option value='" + result[i].areaid + "'>" + result[i].area + "</option>")
                    }
                }
            });
        });
    });


</script>
</html>